package com.bodgittandscarper.brokerhmi.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import static java.util.logging.Level.*;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

import com.bodgittandscarper.broker.types.DatabaseSchema;
import com.bodgittandscarper.broker.types.Record;
import com.bodgittandscarper.brokerhmi.control.BASControl;
import com.bodgittandscarper.brokerhmi.model.HMIModel;
import com.bodgittandscarper.brokerhmi.model.ObservableEvent;
import com.bodgittandscarper.brokerhmi.model.PropertyListener;

/**
 * View class holding Table Panel elements.
 * 
 * @author Morten Rummelhoff
 *
 */
public class TablePanel implements PropertyListener {

	private Logger log = Logger.getLogger(getClass().getName());

	private BASControl basControl;
	private HMIModel hmiModel;

	private JTable table;
	private RecordTableModel recordTableModel;
	
	private JPanel searchPanel;	
	
	public TablePanel(HMIModel hmiModel, BASControl basControl, RecordTableModel tableModel) {
		
		this.hmiModel = hmiModel;
		this.basControl = basControl;

		this.recordTableModel = tableModel;
		
		createGUI();

		this.hmiModel.addPropertyListener(this);
	}

	@Override
	public void propertyChanged(ObservableEvent event, Object value) {
		
		if (event == ObservableEvent.LOCKRECORD_BUTTON_CHANGED) {
			table.setEnabled(!(Boolean) value);
		}
		else if (event == ObservableEvent.SELECTED_RECORD_ALREADY_LOCKED) {
			table.setEnabled(false);
		}
	 
	}
	
	private void createGUI() {
		
		searchPanel = new JPanel(new BorderLayout());
		searchPanel.setBorder(BorderFactory.createTitledBorder("Record(s)"));

		table = new JTable(recordTableModel);
		JScrollPane scrollPane = new JScrollPane(table);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting())
					return;
				
				if (table.getSelectedRow() < 0) {
					hmiModel.notifyPropertyChange(ObservableEvent.SELECT_RECORD_CHANGED, null);
					return;
				}
				
				Record selectedRecord = recordTableModel.getRecord(table.getSelectedRow());

				if (log.isLoggable(INFO))
					log.log(INFO, "SelectedRow: " + selectedRecord);
				
				hmiModel.notifyPropertyChange(ObservableEvent.SELECT_RECORD_CHANGED, selectedRecord);
					
			}
		});
		
		searchPanel.add(scrollPane);
		
		//adjust table columns for dbfield size
		DatabaseSchema dbScheme = basControl.getDatabaseScheme();
		FontMetrics fm = table.getFontMetrics(Font.decode(null));
		
		int charSpaceWidth = fm.charWidth(' ');
		
		for (int i = 0; i < dbScheme.getDatabaseFieldCount(); i++) {
			TableColumn col = table.getColumnModel().getColumn(i);
			col.setPreferredWidth(dbScheme.getDatabaseField(i).getFieldSize()*charSpaceWidth);
		}
		
	}
	
	/**
	 * Gets the presentation of the RecordTable panel.
	 * 
	 * @return Component representation of the RecordTable panel.
	 */
	public JComponent getPresentation() {
		return searchPanel;
	}
	
}
